﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BYManageInfoSystem
{
    public partial class Frm_CargoBoxInfo : Form
    {
        private int m_nSelectBGDComeBackTabID = 0;
        private int m_nSelectCargoBoxTabID = 0;
        
        private DGBY.BLL.GuiXingTab m_bllGuiXingTab = new DGBY.BLL.GuiXingTab();
        private DGBY.BLL.CargoBoxTab m_bllCargoBoxTab = new DGBY.BLL.CargoBoxTab();
        

        private Frm_CargoBoxInfo()
        {
            InitializeComponent();
        }

        public Frm_CargoBoxInfo(int nSelectBGDComeBackTabID)
        {
            InitializeComponent();
            m_nSelectBGDComeBackTabID = nSelectBGDComeBackTabID;
        }



        private void ShowCargoBoxInfoByID(int nSelectBGDComeBackTabID)
        {
          DataSet ds = m_bllCargoBoxTab.GetAllCargoBoxInfo("CargoBoxTab.BGDComeBackTabID = " + nSelectBGDComeBackTabID.ToString());
          if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
          {
              dataGridViewCargoBoxInfo.DataSource = ds.Tables[0];
              dataGridViewCargoBoxInfo.Columns[0].Visible = false;
              dataGridViewCargoBoxInfo.Columns[1].Visible = false;
          }
        }

        private void InitCombox()
        {
            DataSet ds = m_bllGuiXingTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxGuiXing.DataSource = ds.Tables[0];
                this.comboBoxGuiXing.DisplayMember = "柜型";
                this.comboBoxGuiXing.ValueMember = "GuiXingTabID";
                this.comboBoxGuiXing.SelectedIndex = 0;
            }
        }

        private void Frm_CargoBoxInfo_Load(object sender, EventArgs e)
        {
            ShowCargoBoxInfoByID(m_nSelectBGDComeBackTabID);
            InitCombox();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.CargoBoxTab modelCargoBoxTab = new DGBY.Model.CargoBoxTab();

                modelCargoBoxTab.CargoBoxNo = this.textBoxCargoBoxNo.Text;

                if (this.comboBoxGuiXing.SelectedValue != null)
                    modelCargoBoxTab.GuiXingTabID = Convert.ToInt32(this.comboBoxGuiXing.SelectedValue.ToString(), 10);

                modelCargoBoxTab.BGDComeBackTabID = m_nSelectBGDComeBackTabID;

                m_bllCargoBoxTab.Add(modelCargoBoxTab);

                ShowCargoBoxInfoByID(m_nSelectBGDComeBackTabID);

                //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

        }

        private void buttonMod_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.CargoBoxTab modelCargoBoxTabOld = m_bllCargoBoxTab.GetModel(m_nSelectCargoBoxTabID);
                if (modelCargoBoxTabOld == null)
                {
                    MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                DGBY.Model.CargoBoxTab modelCargoBoxTab = new DGBY.Model.CargoBoxTab();
                modelCargoBoxTab.CargoBoxNo = this.textBoxCargoBoxNo.Text;

                if (this.comboBoxGuiXing.SelectedValue != null)
                    modelCargoBoxTab.GuiXingTabID = Convert.ToInt32(this.comboBoxGuiXing.SelectedValue.ToString(), 10);

                modelCargoBoxTab.BGDComeBackTabID = m_nSelectBGDComeBackTabID;
                modelCargoBoxTab.CargoBoxTabID = m_nSelectCargoBoxTabID;

                m_bllCargoBoxTab.Update(modelCargoBoxTab);
                ShowCargoBoxInfoByID(m_nSelectBGDComeBackTabID);
                MessageBox.Show("数据修改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
          
        }

        private void buttonDel_Click(object sender, EventArgs e)
        {
            DGBY.Model.CargoBoxTab modelCargoBoxTabOld = m_bllCargoBoxTab.GetModel(m_nSelectCargoBoxTabID);
            if (modelCargoBoxTabOld == null)
            {
                MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (MessageBox.Show("确实要删除该条记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                m_bllCargoBoxTab.Delete(m_nSelectCargoBoxTabID);

                ShowCargoBoxInfoByID(m_nSelectBGDComeBackTabID);
                MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                this.textBoxCargoBoxNo.Text = "";

                return;
            }
        }

        private void dataGridViewCargoBoxInfo_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string strCargoBoxTabID = this.dataGridViewCargoBoxInfo.Rows[this.dataGridViewCargoBoxInfo.CurrentCell.RowIndex].Cells[0].Value.ToString();
            m_nSelectCargoBoxTabID = Convert.ToInt32(strCargoBoxTabID, 10);
            cellCargoBoxTabClick();
        }
        private void cellCargoBoxTabClick()
        {
            DGBY.Model.CargoBoxTab modelCargoBoxTab = m_bllCargoBoxTab.GetModel(m_nSelectCargoBoxTabID);
            if (modelCargoBoxTab != null)
            {
                this.textBoxCargoBoxNo.Text = modelCargoBoxTab.CargoBoxNo;
                if (modelCargoBoxTab.GuiXingTabID != null)
                    this.comboBoxGuiXing.SelectedValue = modelCargoBoxTab.GuiXingTabID;
            }
        }

        private void dataGridViewCargoBoxInfo_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            {
                using (SolidBrush b = new SolidBrush(dataGridViewCargoBoxInfo.RowHeadersDefaultCellStyle.ForeColor))
                {
                    e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
                }
            }
        }
    }
}